
package com.zsrt.day20170505.homework.ch01;


public class NodeMethodDaoImpl implements NodeMethodDao{

	private Node head;
	public NodeMethodDaoImpl(Node head) {
		this.head = head;
	}
	@Override
	public void addStudent(Student student) {
		//令 p = 头结点
		Node p = this.head;
		Node node = new Node(student);
		while(true){
			
			if(p.getNext() == null){
				p.setNext(node);
				break;
			}else{
				p = p.getNext();
			}
		}
	}

	@Override
	public void insertStudent(Student parentStudent, Student insertsSudent) {
		//令 p = 头结点
		Node p =this.head;
		while(true){
			//找到对应的父节点
			if(p.getNext() !=null){
				
				if(p.getNext().getData().getName().equals(parentStudent.getName())){
					//插入节点
					Node node = new Node(insertsSudent);
					
					node.setNext(p.getNext().getNext());
					
					p.getNext().setNext(node);
					break;
				}else{
					break;
				}
			}
			p = p.getNext();
		}
		
	}

	@Override
	public void printStudent() {
		//令 pNode = 头结点
		Node pNode =this.head;
		//循环遍历
		while(true){
			if(pNode.getNext() != null){
				System.out.println(pNode.getNext().getData());
				
			}else{
				break;
			}
			pNode = pNode.getNext();
		}
	}

	@Override
	public void printGirls() {
		Node p=this.head;
		
		while(true){
			if(p.getNext() != null){
				//判断性别是否为女
				if(p.getNext().getData().getSex() =='女'){
					System.out.println(p.getNext().getData());
				}
			}else{
				break;
			}
			p = p.getNext();
		}
	}

	@Override
	public void deleteStudent(Student student) {
		Node p = this.head;
		while(true){
			if(p.getNext() != null){
				if(p.getNext().getData().getName().equals(student.getName())){
					//删除节点
					p.setNext(p.getNext().getNext());
					break;
				}
			}else{
				System.out.println("空链表");
				break;
			}
			p = p.getNext();
		}
		
	}

	@Override
	public void updateStudent(String studentName, int age) {
		Node pNode = this.head;
		while(true){
			if(pNode.getNext() != null){
				//找到该学生
				if(pNode.getNext().getData().getName().equals(studentName)){
					//修改名字
					pNode.getNext().getData().setAge(age);
					break;
				}
			}else{
				break;
			}
			pNode  = pNode.getNext();
		}
	}

}
